/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.panels;

import java.awt.Dimension;
import org.jfonia.connect5.basics.BasicValueNode;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.connect5.relations.Equal;

/**
 * Extends the abstract class BasicNodePanel and adds observers to the
 * bounds of this component
 *
 * @author Rik Bauwens
 */
public class BasicNodePanel extends NodePanel
{

        public BasicNodePanel(MutableValueNode<Double> xNode, MutableValueNode<Double> yNode,
                     MutableValueNode<Double> widthNode, MutableValueNode<Double> heightNode)
        {
                super(new BasicValueNode<Double>(0.0), new BasicValueNode<Double>(0.0),
                      new BasicValueNode<Double>(0.0), new BasicValueNode<Double>(0.0));
                //Later an offset can be added here
                new Equal(xNode, this.xNode);
                //Later an offset can be added here
                new Equal(yNode, this.yNode);
                //Later an offset can be added here
                new Equal(widthNode, this.widthNode);
                //Later an offset can be added here
                new Equal(heightNode, this.heightNode);
                init();
        }

        private void init()
        {
                //Set bounds of this panel
                setBounds(xNode.getValue().intValue(), yNode.getValue().intValue(),
                          widthNode.getValue().intValue(), heightNode.getValue().intValue());
                setPreferredSize(new Dimension(widthNode.getValue().intValue(), heightNode.getValue().intValue()));
                xNode.addObserver( new Observer() {
			public void onNotify(Object source)
			{
				setLocation(xNode.getValue().intValue(), getY());
			}
		});
                yNode.addObserver( new Observer() {
			public void onNotify(Object source)
			{
				setLocation(getX(), yNode.getValue().intValue());
			}
		});
                widthNode.addObserver( new Observer() {
			public void onNotify(Object source)
			{
				setBounds(getX(), getY(),
                                          widthNode.getValue().intValue(), getHeight());
                                setPreferredSize(new Dimension(widthNode.getValue().intValue(), getHeight()));
			}
		});
                heightNode.addObserver( new Observer() {
			public void onNotify(Object source)
			{
				setBounds(getX(), getY(),
                                          getWidth(), heightNode.getValue().intValue());
                                setPreferredSize(new Dimension(getWidth(), heightNode.getValue().intValue()));
			}
		});
        }
}
